﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Net;

namespace IDAdmin.Lib.Utils
{
    public class MailHelper
    {
        const string SMTP_HOST = "127.0.0.1";
        const string MAIL_SERVER = "mail.gosu2.vn";
        const int MAIL_PORT = 25;
        const string MAIL_USER = "";
        const string MAIL_PASS = "";
        const string MAIL_FROM = "GOSU-ID@game.gosu.vn";
        
        /// <summary>
        /// Gởi email
        /// </summary>
        /// <param name="From">Địa chỉ gởi</param>
        /// <param name="To">Địa chỉ nhận (nếu nhiều thì phân cách bởi dấu ;)</param>
        /// <param name="Subject">Tiêu đề</param>
        /// <param name="Body">Nội dung</param>
        public static void SendMail(string From, string To, string Subject, string Body)
        {
            try
            {
                System.Text.RegularExpressions.Regex regex =
                    new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

                string[] ToArr = To.Split(';');
                if (ToArr.Length == 0)
                {
                    throw new Exception("Địa chỉ email nhận không hợp lệ");
                }
                foreach (string emailAddr in ToArr)
                {
                    if (!regex.IsMatch(emailAddr))
                    {
                        throw new Exception("Địa chỉ email " + emailAddr + " không hợp lệ");                             
                    }
                }

                System.Net.Mail.SmtpClient smtp = new SmtpClient();
                System.Net.Mail.MailMessage msg = new MailMessage(From, To, Subject, Body);
                msg.IsBodyHtml = true;
                smtp.Host = SMTP_HOST;
                smtp.Send(msg);                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void SendMail(string To, string Subject, string Body)
        {
            System.Text.RegularExpressions.Regex regex =
                    new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

            string[] ToArr = To.Split(';');
            if (ToArr.Length == 0)
            {
                throw new Exception("Địa chỉ email nhận không hợp lệ");
            }
            foreach (string emailAddr in ToArr)
            {
                if (!regex.IsMatch(emailAddr))
                {
                    throw new Exception("Địa chỉ email " + emailAddr + " không hợp lệ");
                }
            }

            MailMessage mailMessage = new MailMessage();
            SmtpClient mailClient = new SmtpClient(MAIL_SERVER, MAIL_PORT);
            mailClient.Timeout = 1005000;
            mailClient.Credentials = new NetworkCredential(MAIL_USER, MAIL_PASS);
            mailClient.EnableSsl = false;

            mailMessage.IsBodyHtml = true;
            mailMessage.SubjectEncoding = Encoding.UTF8;
            mailMessage.From = new MailAddress(MAIL_FROM);
            mailMessage.Subject = Subject;
            mailMessage.Body = Body;
            foreach (string emailAddress in ToArr)
            {
                mailMessage.To.Add(emailAddress);
            }

            try
            {
                mailClient.Send(mailMessage);                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
